﻿using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain;
using OnlineMealOrdering.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnlineMealOrdering.Models
{
    public static class EntityToModel
    {

        public static AccountModel ToAccountModel(this UserEntity entity)
        {
            var model = new AccountModel()
            {
                Id = entity.Id,
                UserCode = entity.UserCode,
                Name = entity.Name,
                //Password = entity.Password
            };
            model.Roles = new List<string>();
            model.Latitude = entity.Latitude;
            model.Longitude = entity.Latitude;
            entity.Roles.ToList().ForEach(a => model.Roles.Add(a.RoleCode));

            return model;
        }

        public static FoodCatalogModel ToFoodCatalogMdoel(this FoodCatalogEntity entity)
        {
            var model = new FoodCatalogModel();
            model.Id = entity.Id;
            model.Name = entity.Name;
            if (entity.Parent != null)
                model.ParentId = entity.Parent.Id;
            model.Foods = new List<FoodModel>();
            if (entity.Foods != null)
            {
                entity.Foods.ToList().ForEach(a => { model.Foods.Add(a.ToFoodModel()); });
            }
            return model;
        }

        public static StoreOnlineModel ToStoreOnlineModel(this StoreOnlineEntity entity)
        {
            var model = new StoreOnlineModel();
            model.Address = entity.Address;
            model.BeginBusinessTime = entity.BeginBusinessTime;
            model.Contact = entity.Contact;
            model.Description = entity.Description;
            model.EndBusinessTime = entity.EndBusinessTime;
            model.InitiatePrice = entity.InitiatePrice;
            model.Latitude = entity.Latitude;
            model.Longitude = entity.Longitude;
            model.Name = entity.Name;
            model.PinYin = entity.PinYin;
            model.QQ = entity.QQ;
            model.Status = entity.Status;
            model.Tel = entity.Tel;
            model.Type = entity.Type;
            model.WeiBo = entity.WeiBo;
            model.SortNo = entity.SortNo;
            model.Marks = new List<MarkModel>();
            model.Catalogs = new List<FoodCatalogModel>();
            if (entity.Notices != null && entity.Notices.Count>0)
            {
                model.Note = entity.Notices.ToList().Where(a => a.Type == WebNoteType.OnlineNote).FirstOrDefault().Content;
            }
            if (entity.Images != null && entity.Images.Count > 0)
            {
                model.Image = entity.Images.FirstOrDefault().ThumbnailPath;
            }
            if (entity.Marks != null)
            {

                entity.Marks.ToList().ForEach(a => { model.Marks.Add(a.ToMarkModel()); });
            }
            if (entity.Catalogs != null)
            {
                entity.Catalogs.ToList().ForEach(a =>
                {
                    model.Catalogs.Add(a.ToFoodCatalogMdoel());
                });
            }


            model.Id = entity.Id;


            return model;
        }

        public static FoodModel ToFoodModel(this FoodEntity entity)
        {
            var model = new FoodModel();
            model.Id = entity.Id;
            model.Name = entity.Name;
            model.Price = entity.Price;
            if (entity.Images != null && entity.Images.Count > 0)
                model.Image = entity.Images.FirstOrDefault().ThumbnailPath;
            model.CatalogName = entity.Catalog.Name;
            return model;

        }

        public static MarkModel ToMarkModel(this MarkEntity entity)
        {
            var model = new MarkModel();
            model.Name = entity.Name;
            model.Image = entity.Image==null?"":entity.Image.Path;
            return model;

        }


    }
}